﻿using Microsoft.AspNetCore.Mvc;
using SD.Infrastructure.DTOBase;
using SD.Toolkits.AspNetCore.Attributes;
using SlamDunk.MES.IAppService.DTOs.Inputs;
using SlamDunk.MES.IAppService.DTOs.Outputs.ExecutionContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Host.Controllers
{
    /// <summary>
    /// 作业管理服务WebApi接口
    /// </summary>
    [ApiController]
    [Route("Api/[controller]/[action]")]
    public class ExecutionController : ControllerBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 作业管理服务契约接口
        /// </summary>
        private readonly IExecutionContract _executionContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ExecutionController(IExecutionContract executionContract)
        {
            this._executionContract = executionContract;
        }

        #endregion


        //命令部分

        #region # 生成工单 —— void GenerateWorkOrders(Guid executionPlanId)
        /// <summary>
        /// 生成工单
        /// </summary>
        /// <param name="executionPlanId">作业计划Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void GenerateWorkOrders(Guid executionPlanId)
        {
            this._executionContract.GenerateWorkOrders(executionPlanId);
        }
        #endregion

        #region # 修改工单 —— void UpdateWorkOrder(Guid workOrderId, string workOrderName...
        /// <summary>
        /// 修改工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="workOrderName">工单名称</param>
        /// <param name="durationQuota">工时定额</param>
        /// <param name="directivePath">工艺指导书</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="workStationId">工位Id</param>
        /// <param name="needToInspect">是否需质检</param>
        /// <param name="sort">排序</param>
        /// <param name="description">描述</param>
        [HttpPost]
        [WrapPostParameters]
        public void UpdateWorkOrder(Guid workOrderId, string workOrderName, TimeSpan durationQuota, string directivePath, Guid? facilityId, Guid? workStationId, bool needToInspect, int sort, string description)
        {
            this._executionContract.UpdateWorkOrder(workOrderId, workOrderName, durationQuota, directivePath, facilityId, workStationId, needToInspect, sort, description);
        }
        #endregion

        #region # 保存工单人员要求 —— void SaveWorkOrderPersonAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单人员要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="personAskParams">工单人员要求参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderPersonAsks(Guid workOrderId, IEnumerable<WorkOrderPersonAskParam> personAskParams)
        {
            this._executionContract.SaveWorkOrderPersonAsks(workOrderId, personAskParams);
        }
        #endregion

        #region # 保存工单设备要求 —— void SaveWorkOrderEquipmentAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单设备要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="equipmentAskParams">工单设备要求参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderEquipmentAsks(Guid workOrderId, IEnumerable<WorkOrderEquipmentAskParam> equipmentAskParams)
        {
            this._executionContract.SaveWorkOrderEquipmentAsks(workOrderId, equipmentAskParams);
        }
        #endregion

        #region # 保存工单物料要求 —— void SaveWorkOrderMaterialAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单物料要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="materialAskParams">工单物料要求参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderMaterialAsks(Guid workOrderId, IEnumerable<WorkOrderMaterialAskParam> materialAskParams)
        {
            this._executionContract.SaveWorkOrderMaterialAsks(workOrderId, materialAskParams);
        }
        #endregion

        #region # 保存工单参数要求 —— void SaveWorkOrderParameterAsks(Guid workOrderId...
        /// <summary>
        /// 保存工单参数要求
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="parameterAskParams">工单参数要求参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderParameterAsks(Guid workOrderId, IEnumerable<WorkOrderParameterAskParam> parameterAskParams)
        {
            this._executionContract.SaveWorkOrderParameterAsks(workOrderId, parameterAskParams);
        }
        #endregion

        #region # 准备工单 —— void ReadyWorkOrder(Guid workOrderId)
        /// <summary>
        /// 准备工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void ReadyWorkOrder(Guid workOrderId)
        {
            this._executionContract.ReadyWorkOrder(workOrderId);
        }
        #endregion

        #region # 取消工单 —— void CancelWorkOrder(string reason)
        /// <summary>
        /// 取消工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="reason">取消原因</param>
        [HttpPost]
        [WrapPostParameters]
        public void CancelWorkOrder(Guid workOrderId, string reason)
        {
            this._executionContract.CancelWorkOrder(workOrderId, reason);
        }
        #endregion

        #region # 分派工单到班组 —— void DispatchWorkOrderToTeam(Guid workOrderId, Guid teamId)
        /// <summary>
        /// 分派工单到班组
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="teamId">班组Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void DispatchWorkOrderToTeam(Guid workOrderId, Guid teamId)
        {
            this._executionContract.DispatchWorkOrderToTeam(workOrderId, teamId);
        }
        #endregion

        #region # 分派工单到人员 —— void DispatchWorkOrderToPerson(Guid workOrderId, Guid personId)
        /// <summary>
        /// 分派工单到人员
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="personId">人员Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void DispatchWorkOrderToPerson(Guid workOrderId, Guid personId)
        {
            this._executionContract.DispatchWorkOrderToPerson(workOrderId, personId);
        }
        #endregion

        #region # 执行工单 —— void ExecuteWorkOrder(Guid workOrderId)
        /// <summary>
        /// 执行工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void ExecuteWorkOrder(Guid workOrderId)
        {
            this._executionContract.ExecuteWorkOrder(workOrderId);
        }
        #endregion

        #region # 中止工单 —— void AbortWorkOrder(Guid workOrderId, string reason)
        /// <summary>
        /// 中止工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="reason">中止原因</param>
        [HttpPost]
        [WrapPostParameters]
        public void AbortWorkOrder(Guid workOrderId, string reason)
        {
            this._executionContract.AbortWorkOrder(workOrderId, reason);
        }
        #endregion

        #region # 完成工单 —— void CompleteWorkOrder(Guid workOrderId)
        /// <summary>
        /// 完成工单
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        [HttpPost]
        [WrapPostParameters]
        public void CompleteWorkOrder(Guid workOrderId)
        {
            this._executionContract.CompleteWorkOrder(workOrderId);
        }
        #endregion

        #region # 保存工单实际人员 —— void SaveWorkOrderActualPersons(Guid workOrderId...
        /// <summary>
        /// 保存工单实际人员
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualPersonParams">工单实际人员参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderActualPersons(Guid workOrderId, IEnumerable<WorkOrderActualPersonParam> actualPersonParams)
        {
            this._executionContract.SaveWorkOrderActualPersons(workOrderId, actualPersonParams);
        }
        #endregion

        #region # 保存工单实际设备 —— void SaveWorkOrderActualEquipments(Guid workOrderId...
        /// <summary>
        /// 保存工单实际设备
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualEquipmentParams">工单实际设备参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderActualEquipments(Guid workOrderId, IEnumerable<WorkOrderActualEquipmentParam> actualEquipmentParams)
        {
            this._executionContract.SaveWorkOrderActualEquipments(workOrderId, actualEquipmentParams);
        }
        #endregion

        #region # 保存工单实际物料投入 —— void SaveWorkOrderActualInputMaterials(Guid workOrderId...
        /// <summary>
        /// 保存工单实际物料投入
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualMaterialInputParams">工单实际物料投入参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderActualInputMaterials(Guid workOrderId, IEnumerable<WorkOrderActualMaterialParam> actualMaterialInputParams)
        {
            this._executionContract.SaveWorkOrderActualInputMaterials(workOrderId, actualMaterialInputParams);
        }
        #endregion

        #region # 保存工单实际物料产出 —— void SaveWorkOrderActualOutputMaterials(Guid workOrderId...
        /// <summary>
        /// 保存工单实际物料产出
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualOutputMaterialParams">工单实际物料产出参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderActualOutputMaterials(Guid workOrderId, IEnumerable<WorkOrderActualMaterialParam> actualOutputMaterialParams)
        {
            this._executionContract.SaveWorkOrderActualOutputMaterials(workOrderId, actualOutputMaterialParams);
        }
        #endregion

        #region # 保存工单实际参数 —— void SaveWorkOrderActualParameters(Guid workOrderId...
        /// <summary>
        /// 保存工单实际参数
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <param name="actualParameterParams">工单实际参数参数模型集</param>
        [HttpPost]
        [WrapPostParameters]
        public void SaveWorkOrderActualParameters(Guid workOrderId, IEnumerable<WorkOrderActualParameterParam> actualParameterParams)
        {
            this._executionContract.SaveWorkOrderActualParameters(workOrderId, actualParameterParams);
        }
        #endregion


        //查询部分

        #region # 分页获取工单列表 —— PageModel<WorkOrderInfo> GetWorkOrdersByPage(string keywords...
        /// <summary>
        /// 分页获取工单列表
        /// </summary>
        /// <param name="keywords">关键字</param>
        /// <param name="productionPlanId">生产计划Id</param>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <param name="processSegmentId">工艺段Id</param>
        /// <param name="workStationId">工位Id</param>
        /// <param name="needToInspect">是否需质检</param>
        /// <param name="hasInspected">是否已质检</param>
        /// <param name="dispatchedTeamId">分派班组Id</param>
        /// <param name="dispatchedPersonId">分派人员Id</param>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <param name="facilityId">工厂模型Id</param>
        /// <param name="statuses">工单状态集</param>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页容量</param>
        /// <returns>工单列表</returns>
        [HttpGet]
        public PageModel<WorkOrderInfo> GetWorkOrdersByPage(string keywords, Guid? productionPlanId, Guid? executionPlanId, Guid? processSegmentId, Guid? workStationId, bool? needToInspect, bool? hasInspected, Guid? dispatchedTeamId, Guid? dispatchedPersonId, DateTime? startTime, DateTime? endTime, Guid? facilityId, [FromJson] IList<WorkOrderStatus> statuses, int pageIndex, int pageSize)
        {
            return this._executionContract.GetWorkOrdersByPage(keywords, productionPlanId, executionPlanId, processSegmentId, workStationId, needToInspect, hasInspected, dispatchedTeamId, dispatchedPersonId, startTime, endTime, facilityId, statuses, pageIndex, pageSize);
        }
        #endregion

        #region # 获取工单人员要求列表 —— IEnumerable<WorkOrderPersonAskInfo> GetWorkOrderPersonAsks(...
        /// <summary>
        /// 获取工单人员要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单人员要求列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderPersonAskInfo> GetWorkOrderPersonAsks(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderPersonAsks(workOrderId);
        }
        #endregion

        #region # 获取工单设备要求列表 —— IEnumerable<WorkOrderEquipmentAskInfo> GetWorkOrderEquipmentAsks(...
        /// <summary>
        /// 获取工单设备要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单设备要求列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderEquipmentAskInfo> GetWorkOrderEquipmentAsks(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderEquipmentAsks(workOrderId);
        }
        #endregion

        #region # 获取工单物料要求列表 —— IEnumerable<WorkOrderMaterialAskInfo> GetWorkOrderMaterialAsks(...
        /// <summary>
        /// 获取工单物料要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单物料要求列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderMaterialAskInfo> GetWorkOrderMaterialAsks(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderMaterialAsks(workOrderId);
        }
        #endregion

        #region # 获取工单参数要求列表 —— IEnumerable<WorkOrderParameterAskInfo> GetWorkOrderParameterAsks(...
        /// <summary>
        /// 获取工单参数要求列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单参数要求列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderParameterAskInfo> GetWorkOrderParameterAsks(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderParameterAsks(workOrderId);
        }
        #endregion

        #region # 获取工单实际人员列表 —— IEnumerable<WorkOrderActualPersonInfo> GetWorkOrderActualPersons(...
        /// <summary>
        /// 获取工单实际人员列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际人员列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderActualPersonInfo> GetWorkOrderActualPersons(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderActualPersons(workOrderId);
        }
        #endregion

        #region # 获取工单实际设备列表 —— IEnumerable<WorkOrderActualEquipmentInfo> GetWorkOrderActualEquipments(...
        /// <summary>
        /// 获取工单实际设备列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际设备列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderActualEquipmentInfo> GetWorkOrderActualEquipments(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderActualEquipments(workOrderId);
        }
        #endregion

        #region # 获取工单实际物料列表 —— IEnumerable<WorkOrderActualMaterialInfo> GetWorkOrderActualMaterials(...
        /// <summary>
        /// 获取工单实际物料列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际物料列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderActualMaterialInfo> GetWorkOrderActualMaterials(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderActualMaterials(workOrderId);
        }
        #endregion

        #region # 获取工单实际参数列表 —— IEnumerable<WorkOrderActualParameterInfo> GetWorkOrderActualParameters(...
        /// <summary>
        /// 获取工单实际参数列表
        /// </summary>
        /// <param name="workOrderId">工单Id</param>
        /// <returns>工单实际参数列表</returns>
        [HttpGet]
        public IEnumerable<WorkOrderActualParameterInfo> GetWorkOrderActualParameters(Guid workOrderId)
        {
            return this._executionContract.GetWorkOrderActualParameters(workOrderId);
        }
        #endregion
    }
}
